/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.autocheck.impl;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.appmoblie.RefundSegDto;
import com.ceair.flight.refund.service.cronjob.autocheck.IRescheduleChcker;
import com.ceair.flight.refund.service.cronjob.autocheck.IScheduleChangeChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Set;

/**
 * 检查是否航变
 *
 * @author youguilin
 */
@Service
public class ScheduleChangeChecker implements IScheduleChangeChecker {

    private static final Logger LOG = LoggerFactory.getLogger(ScheduleChangeChecker.class);
    @Autowired
    private IRescheduleChcker rescheduleChcker;

    /**
     * 检查是否申请单中的所有的航班是否有航变航班
     *
     * @param refundApply
     * @return
     */
    public boolean isScheduleChangeFlight(RefundApplyDto refundApply) {
        boolean isChange = false;
        try {
            Set<RefundSegDto> segments = refundApply.getRefundTkt().getRefundSegs();
            LOG.debug("Checking refund apply {} if the flights are reschedule", refundApply.getId());
            for (RefundSegDto segment : segments) {
                String flightNo = segment.getFlightNo();
                Date depDate = segment.getDepartDate();
                String depAirport = segment.getDepartAirport();
                String arrAirport = segment.getArriveAirport();
                isChange = rescheduleChcker.isReschedule(flightNo, depDate, depAirport, arrAirport);
                //所有的航班中如果有一段发生航变则整个票中的航段是航变成立
                if (isChange) {
                    LOG.info("Refund apply {} is verfied to be flight reschuled on flight No {}", refundApply.getId(), flightNo);
                    break;
                }
            }
        } catch (Exception e) {
            LOG.error("Checking refund apply {} if the flights are reschedule error", refundApply.getId(), e);
        }
        return isChange;
    }

}
